import React from 'react';
import PropTypes from 'prop-types';
import {Card, Button, Row, Col} from 'antd';
import {FORM_OPTIONS} from '../constants';
import DescriptionList from 'ant-design-pro/lib/DescriptionList';
import routes from './routes';
import {injectIntl} from './intl';
import {saleOrderPriceTerms, paymentType, currency, saleOrderTransportType} from '../Enum';
import {conventEnumValueToString, formatAmount} from '../utils';
import FileUpload from './FileUpload';

const {Description} = DescriptionList;

export class ClearancePanel extends React.PureComponent {
    state = {
        hasOperat: false,
        loading: false,
        data: {},
    }
    componentDidMount() {
        this.props.init();
    }

    omponentWillUnmount() {
        this.isunmount = true;
    }
    onChangeUpload = value => {
        if(!this.state.hasOperat)
            this.setState({
                hasOperat: true
            });
        this.props.updateAttachmentsClear(value);
    }
    onClearanceSubmit = () => {
        this.setState({
            loading: true,
        });
        this.props.onClearanceSubmit(this.props.id, this.state.data).then(() => {
            if(this.isunmount)
                this.setState({loading: false});
        });
    }
    
    render() {
        const detailData = this.props.detail || [];
        const {clearanceAttachments = []} = this.props.detail;
        const {formatMessage} = this.props.intl;
        return (
            <div className="detail">
                <Card loading={this.props.loading}>
                    <DescriptionList size="large" title={<Row><Col span={6}>{formatMessage({
                        id: 'clearancePanel.text.theme',
                        defaultMessage: '销售确认书主单'
                    })}</Col>
                    </Row>}>
                        <Description term={formatMessage({
                            id: 'clearancePanel.text.code',
                            defaultMessage: '销售确认书号'
                        })}>{detailData.code}
                        </Description>
                        <Description term={formatMessage({
                            id: 'clearancePanel.text.brandName',
                            defaultMessage: '品牌'
                        })}>{detailData.brandName}
                        </Description>
                        <Description term={formatMessage({
                            id: 'clearancePanel.text.dealer',
                            defaultMessage: '总代'
                        })}>{`(${detailData.dealerCode})${detailData.dealerName}`}
                        </Description>
                        <Description term={formatMessage({
                            id: 'clearancePanel.text.tradeMethodName',
                            defaultMessage: '贸易方式'
                        })}>{detailData.tradeMethodName}
                        </Description>
                        <Description term={formatMessage({
                            id: 'clearancePanel.text.salesOrderPriceTerms',
                            defaultMessage: '价格条款'
                        })}>{conventEnumValueToString(saleOrderPriceTerms, detailData.salesOrderPriceTerms)}
                        </Description>
                        <Description term={formatMessage({
                            id: 'clearancePanel.text.payment',
                            defaultMessage: '付款方式'
                        })}>{conventEnumValueToString(paymentType, detailData.payment)}
                        </Description>
                        <Description term={formatMessage({
                            id: 'clearancePanel.text.letterOfCreditCode',
                            defaultMessage: '信用证号'
                        })}>{detailData.letterOfCreditCode}
                        </Description>
                        <Description term={formatMessage({
                            id: 'clearancePanel.text.LcVersionCode',
                            defaultMessage: '信用证版本号'
                        })}>{detailData.LcVersionCode}
                        </Description>
                        {(detailData.salesOrderPriceTerms === 2 || detailData.salesOrderPriceTerms === 3 ||
                            detailData.salesOrderPriceTerms === 5 || detailData.salesOrderPriceTerms === 6) &&
                        <Description term={formatMessage({
                            id: 'clearancePanel.text.shipmentAmount',
                            defaultMessage: '运费'
                        })}>{formatAmount(detailData.shipmentAmount)}
                        </Description>
                        }
                        {(detailData.salesOrderPriceTerms === 3 || detailData.salesOrderPriceTerms === 6) &&
                        <Description term={formatMessage({
                            id: 'clearancePanel.text.insuranceAmount',
                            defaultMessage: '保费'
                        })}>{formatAmount(detailData.insuranceAmount)}
                        </Description>
                        }
                        <Description term={formatMessage({
                            id: 'clearancePanel.text.totalAmount',
                            defaultMessage: '总金额'
                        })}>{formatAmount(detailData.totalAmount)}
                        </Description>
                        <Description term={formatMessage({
                            id: 'clearancePanel.text.currency',
                            defaultMessage: '币种'
                        })}>{conventEnumValueToString(currency, detailData.currency)}
                        </Description>
                        <Description term={formatMessage({
                            id: 'clearancePanel.text.transportType',
                            defaultMessage: '运输方式'
                        })}>{conventEnumValueToString(saleOrderTransportType, detailData.transportType)}
                        </Description>
                        <Description term={formatMessage({
                            id: 'clearancePanel.text.shippingPortName',
                            defaultMessage: '发运港口'
                        })}>{detailData.shippingPortName}
                        </Description>
                        <Description term={formatMessage({
                            id: 'clearancePanel.text.destinationPort',
                            defaultMessage: '目的港口'
                        })}>{detailData.destinationPort}
                        </Description>
                        <Description term={formatMessage({
                            id: 'clearancePanel.text.batchNumber',
                            defaultMessage: '批次号'
                        })}>{detailData.batchNumber}
                        </Description>
                    </DescriptionList>
                </Card>

                <Card title={formatMessage({
                    id: 'clearancePanel.title.clearanceAttachments',
                    defaultMessage: '清关附件清单'
                })}>
                    <Row>
                        <Col {...FORM_OPTIONS.col}>
                            <FileUpload onChange={this.onChangeUpload} onRemove={this.props.removeAttachmentsClear} fileList={clearanceAttachments}/>
                        </Col>
                    </Row>
                </Card>

                <Card>
                    <Row>
                        <Col className="col-align-right">
                            <Button key="audit"
                                onClick={this.onClearanceSubmit}
                                type="primary"
                                loading={this.state.loading}>
                                {formatMessage({
                                    id: 'clearancePanel.btn.save',
                                    defaultMessage: '保存'
                                })}</Button>
                        </Col>
                    </Row>
                </Card>
                
                <div className="page-toolbar">
                    <Button
                        type="primary"
                        disabled={this.props.loading}
                        onClick={this.props.onCancel}>{formatMessage({
                            id: 'clearancePanel.text.return',
                            defaultMessage: '返回'
                        })}</Button>
                </div>
            </div>
        );
    }
}

ClearancePanel.propTypes = {
    detail: PropTypes.object.isRequired,
    id: PropTypes.string.isRequired,
    init: PropTypes.func.isRequired,
    loading: PropTypes.bool.isRequired,
    onCancel: PropTypes.func.isRequired,
    onClearanceSubmit: PropTypes.func.isRequired,
    history: PropTypes.object,
    intl: PropTypes.object,
    successReturn: PropTypes.func,
};

import {connect} from 'react-redux';
import {
    onClearanceSubmit,
    getDetail,
    tableSearch,
    updateAttachmentsClear,
    removeAttachmentsClear
} from './actions';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
const getDetailData = selectorFactory(['page', 'domainData', 'detail', 'data']);

const mapStateToProps = (state, props) => ({
    id: props.id,
    loading: state.getIn(['page', 'domainData', 'detail', 'isFetching']),
    detail: getDetailData(state),
});
const mapDispatchToProps = (dispatch, props) => ({
    init: () => dispatch(getDetail(props.id)),
    successReturn: () => {
        dispatch(tableSearch());
        props.history.push(routes.query.url());
    },
    onClearanceSubmit: (id, data) => dispatch(onClearanceSubmit(id, data)).then(res => {
        if(res.success) {
            dispatch(tableSearch());
            props.history.push(routes.query.url());
        }
    }),
    onCancel: () => {
        props.history.push(routes.query.url());
    },
    updateAttachmentsClear: data => dispatch(updateAttachmentsClear(data)),
    removeAttachmentsClear: ({id}) => dispatch(removeAttachmentsClear(id)),
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(ClearancePanel));
